package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;
import java.util.UUID;

public class MyCenterPage extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public static ForumListPage forumListPage;
    public static SignUpPage signUpPage;
    public static WebDriverWait wait;
    public MyCenterPage() {
        super(url);
        forumListPage = new ForumListPage();
        signUpPage = new SignUpPage();
        wait = new WebDriverWait(driver, Duration.ofSeconds(5));
    }

    private void freshPage () {
        driver.get("http://127.0.0.1:58080/index.html");
        // 进入个人中心
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();
    }
    /**
     * 昵称不进行修改
     */
    public void modifyNickNameFail () throws InterruptedException {
        driver.findElement(By.cssSelector("#setting_input_nickname"));
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败");
    }

    /**
     * 昵称长度过长
     */
    public void modifyNickNameFail1 () {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_nickname")).clear();
        driver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys("22222222222222222222222222222222222222222222222222222222222222222222222222222222");
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败");
    }

    /**
     * 昵称正常修改
     */
    public void modifyNickNameSuccess () {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_nickname")).clear();
        driver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys(UUID.randomUUID().toString().substring(0,10));
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("操作成功");
    }

    /**
     * 邮箱不进行修改提交
     */
    public void modifyEmailFail () throws InterruptedException {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_email"));
        driver.findElement(By.cssSelector("#setting_submit_email")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败");
    }

    /**
     * 邮箱长度过长
     */
    public void modifyEmailFail1 () {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_email")).clear();
        driver.findElement(By.cssSelector("#setting_input_email")).sendKeys("22222222222222222222222222222222222222222222222222222222222222222222222222222222");
        driver.findElement(By.cssSelector("#setting_submit_email")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败");
    }

    /**
     * 邮箱正常修改
     */
    public void modifyEmailSuccess () {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_email")).clear();
        driver.findElement(By.cssSelector("#setting_input_email")).sendKeys(UUID.randomUUID().toString().substring(0,9) + "@" + "qq.com");
        driver.findElement(By.cssSelector("#setting_submit_email")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("操作成功");
    }


    /**
     * 电话保持原来的不变
     */
    public void modifyPhoneNumberFail () {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_phoneNum"));
        driver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败");
    }


    /**
     * 电话长度过长
     */
    public void modifyPhoneNumberFail1 () {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).clear();
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys("11111111111111111111111111111111111111111111");
        driver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败");
    }

    /**
     * 电话保持正确的格式并且输入成功并修改
     */
    public void modifyPhoneNumberSuccess () {
        freshPage();
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).clear();
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys(String.valueOf(10000000000L + (long)(Math.random() * 90000000000L)));
        driver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("操作成功");
    }

    /**
     * 修改密码失败
     * 原密码为空 / 新密码为空 / 确认密码为空
     * 原密码不为空 / 新密码不为空 / 确认密码为空
     * 原密码不为空 / 新密码为空 / 确认密码不为空
     */
    public void modifyPasswordFail () throws InterruptedException {
        freshPage();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(500);
        // 原密码为空 / 新密码为空 / 确认密码为空
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear(); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear(); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear(); // 确认密码
        // 原密码不为空 / 新密码不为空 / 确认密码为空
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("6"); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("66"); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")); // 确认密码
        // 原密码不为空 / 新密码为空 / 确认密码不为空
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear(); // 原密码
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("6"); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear(); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("66"); // 确认密码
    }

    /**
     * 修改密码失败
     * 原密码不正确/确认密码正确/密码正确
     */
    public void modifyPasswordFail1 () throws InterruptedException {
        freshPage();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
//        wait.until(new ExpectedCondition<Boolean>() {
//            @Override
//            public Boolean apply(WebDriver d) {
//                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
//                        "return window.pageYOffset + window.innerHeight;");
//                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
//                        "return document.body.scrollHeight;");
//                double pos = scrollPosition.doubleValue();
//                double height = scrollHeight.doubleValue();
////                System.out.println("Scroll position: " + pos + ", Scroll height: " + height);
//                // 允许1像素的误差
//                return Math.abs(pos - height) < 2;
//            }
//        });
        Thread.sleep(1000);
        // 清空一下
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear(); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear(); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear(); // 确认密码
        // 原密码不正确/确认密码正确/密码正确
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("6"); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("66"); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("66"); // 确认密码
        // 点击提交修改
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#settings_submit_password")));
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        // 判断是否操作出错
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("操作失败");
    }

    /**
     * 修改密码失败
     * 原密码正确/确认密码/密码不一致
     */
    public void modifyPasswordFail2 () throws InterruptedException {
        forumListPage.checkMyCenter();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
//        // 使用 WebDriverWait 等待直到页面滚动到底部
//        wait.until(new ExpectedCondition<Boolean>() {
//            @Override
//            public Boolean apply(WebDriver d) {
//                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
//                        "return window.pageYOffset + window.innerHeight;");
//                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
//                        "return document.body.scrollHeight;");
//                double pos = scrollPosition.doubleValue();
//                double height = scrollHeight.doubleValue();
//                // 允许1像素的误差
//                return Math.abs(pos - height) < 2;
//            }
//        });
        Thread.sleep(500);
        // 清空一下
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear(); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear(); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear(); // 确认密码
        // 原密码正确/确认密码/密码不一致
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("1"); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("661"); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("66"); // 确认密码
        // 点击修改密码
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("两次输入的密码不相同");
    }

    /**
     * 修改密码失败
     * 原密码不正确/确认密码/密码不一致
     */
    public void modifyPasswordFail3 () {
        forumListPage.checkMyCenter();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                double pos = scrollPosition.doubleValue();
                double height = scrollHeight.doubleValue();
                // 允许1像素的误差
                return Math.abs(pos - height) < 1;
            }
        });
        // 清空一下
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear(); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear(); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear(); // 确认密码
        // 原密码不正确/确认密码/密码不一致
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("11111"); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("668"); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("66"); // 确认密码
        // 点击修改密码
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        // 判断修改是否有误
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("两次输入的密码不相同");
    }

    /**
     * 修改密码成功
     */
    public void modifyPasswordSuccess () throws InterruptedException {
        forumListPage.checkMyCenter();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                double pos = scrollPosition.doubleValue();
                double height = scrollHeight.doubleValue();
                // 允许1像素的误差
                return Math.abs(pos - height) < 2;
            }
        });
        // 清空一下
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear(); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear(); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear(); // 确认密码
        // 原密码不正确/确认密码/密码不一致
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("1"); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("6"); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("6"); // 确认密码
        // 点击修改密码
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
    }


    public void modifyPasswordSuccess1 () throws InterruptedException {
        forumListPage.checkMyCenter();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                double pos = scrollPosition.doubleValue();
                double height = scrollHeight.doubleValue();
                // 允许1像素的误差
                return Math.abs(pos - height) < 2;
            }
        });
        // 清空一下
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear(); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear(); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear(); // 确认密码
        // 原密码不正确/确认密码/密码不一致
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("6"); // 原密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("1"); // 新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("1"); // 确认密码
        // 点击修改密码
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        // 修改成功会跳到登录界面
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#submit")));
    }
}
